import customTab from "../../../wxjs/customTab";
const app = getApp();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  xbodyMsg: "请稍等...",
};

var currentPage = {
  data,
  onLoad,
  getDetail,
  getSearchLabelByType,
  edit,
};

var page = Object.assign({}, currentPage);

Page(page);

function onLoad() {
  const that = this;
  that.getSearchLabelByType();
  that.getDetail();
}

function getDetail() {
  const that = this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.applyDetail,
    cancelToast: true,
    success: function(res) {
      that.setData({
        xbodyMsg: "",
        form: res.data
      });
      that.form = res.data;
    },
    fail: function(res) {
      if (res.code == 202) {
        wx.redirectTo({
          url: app.xhx_route.applyForm,
        })
      } else {
        that.setData({
          xbodyMsg: res && res.msg || "请求失败"
        });
      }
    }
  })
}

function getSearchLabelByType() {
  const that = this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.getSearchLabelByType,
    data: {
      type: 2
    },
    cancelToast: true,
    success: function(res) {
      let types = res.data;
      let temp = {
        types
      };
      that.setData(temp);
    },
    fail: function(res) {

    }
  })
}

function edit() {
  const that = this;
  const form = that.data.form;
  let temp = {};

  for (let key in form) {
    let arr = key.split();
    temp["xform_" + key] = form[key];
  }

  wx.redirectTo({
    url: `${app.xhx_route.applyForm}?json=` + JSON.stringify(temp),
  })
}